const activesModel = require("../models/activesModel")
// 引入dayjs
const dayjs = require('dayjs');
// jwt--token操作
const jwt = require('jsonwebtoken');

// 1.活动列表
exports.activesList = async (req, res) => {
    let { page, pageSize, name, location } = req.query
    if (!page && !pageSize) {
        res.status(400).json({
            code: 400,
            msg: "缺少必要的参数"
        })
    }
    try {
        let results = await activesModel.activesList(Number(page), Number(pageSize), name, location)
        console.log("控制器：：：：：", results)
        res.json({ code: 0, msg: "活动列表请求成功！", results: results })
    } catch (error) {
        console.log(error)
        res.json({ code: -1, msg: "活动列表请求失败！", error })
    }
}

// 2.添加活动
exports.addactive = async (req, res) => {
    let { name, location, detail, time, latitude, longitude, talk, pic, publisher } = req.body
    if (!name || !location || !detail || !time || !latitude || !longitude || !talk || !pic || !publisher) {
        return res.status(400).json({ code: -1, msg: '缺少必要的参数' });
    }
    // 从请求头中获取Authorization字段，并提取token----获取添加用户的id
    const authHeader = req.headers.authorization;
    const token = authHeader ? authHeader.split(' ')[1] : null; // 提取token
    console.log('未处理：', authHeader, '处理的：', token)
    if (!token) {
        return res.status(401).json({ code: 401, msg: "请提供token" });
    }
    const decoded = jwt.verify(token, '39qw89r23890');
    const { id } = decoded;
    let publisher_id = id;
    // 获取当前时间
    const now = dayjs();
    console.log(now.format('YYYY-MM-DD HH:mm:ss')); // 2023-08-15 14:30:45
    const now_dateTime = now.format('YYYY-MM-DD HH:mm:ss')
    try {
        const results = await activesModel.addactive(name, location, detail, latitude, longitude, talk, time, now_dateTime, pic, publisher_id, publisher)
        // 判断更新结果
        if (!results || results.affectedRows === 0) { // 根据ORM具体返回值判断
            return res.status(400).json({
                code: 400,
                msg: "更新失败：活动不存在或数据未变化",
            });
        }
        res.json({ code: 0, msg: "添加活动请求成功！", results })
    } catch (error) {
        console.log(error)
        res.json({ code: -1, msg: "添加活动请求失败！", error })
    }
}

// 3.删除活动---更新记录状态
exports.Fdelete = async (req, res) => {
    const { id } = req.body
    try {
        // 获取当前时间
        const now = dayjs();
        console.log(now.format('YYYY-MM-DD HH:mm:ss')); // 2023-08-15 14:30:45
        const now_dateTime = now.format('YYYY-MM-DD HH:mm:ss')
        const results = await activesModel.Fdelete(Number(id), now_dateTime)
        // 判断更新结果
        if (!results || results.affectedRows === 0) { // 根据ORM具体返回值判断
            return res.status(400).json({
                code: 400,
                msg: "更新失败：活动不存在或数据未变化",
            });
        }
        res.json({ code: 0, msg: "删除活动请求成功！", results })
    } catch (error) {
        console.log(error)
        res.json({ code: -1, msg: "添加活动请求失败！", error })
    }
}

// 4.删除活动---删除记录状态
exports.Tdelete = async (req, res) => {
    const { id } = req.body
    try {
        const results = await activesModel.Tdelete(Number(id))
        // 判断更新结果
        if (!results || results.affectedRows === 0) { // 根据ORM具体返回值判断
            return res.status(400).json({
                code: 400,
                msg: "更新失败：活动不存在或数据未变化",
            });
        }
        res.json({ code: 0, msg: "删除活动请求成功！", results })
    } catch (error) {
        console.log(error)
        res.json({ code: -1, msg: "添加活动请求失败！", error })
    }
}

// 5.获取修改数据
exports.get = async (req, res) => {
    const { id } = req.query

    try {
        const results = await activesModel.get(Number(id))
        // 判断更新结果
        if (!results || results.affectedRows === 0) { // 根据ORM具体返回值判断
            return res.status(400).json({
                code: 400,
                msg: "获取失败：活动不存在或数据未变化",
            });
        }
        res.json({ code: 0, msg: "获取修改数据请求成功！", results })
    } catch (error) {
        console.log(error)
        res.json({ code: -1, msg: "获取修改数据请求失败！", error })
    }
}


// 6.更新记录数据
exports.set = async (req, res) => {
    const { id, name, location, detail, time, latitude, longitude, talk, pic, publisher } = req.body
    if (!id) {
        return res.status(400).json({ code: -1, msg: '缺少必要的参数:id' });
    }
    if (!name || !location || !detail || !time || !latitude || !longitude || !talk || !pic || !publisher) {
        return res.status(400).json({ code: -1, msg: '缺少必要的参数' });
    }
    try {
        const results = await activesModel.set(Number(id), name, location, detail, time, latitude, longitude, talk, pic, publisher)
        // 判断更新结果
        if (!results || results.affectedRows === 0) { // 根据ORM具体返回值判断
            return res.status(400).json({
                code: 400,
                msg: "获取失败：活动不存在或数据未变化",
            });
        }
        res.json({ code: 0, msg: "更新记录数据请求成功！", results })
    } catch (error) {
        console.log(error)
        res.json({ code: -1, msg: "更新记录数据请求失败！", error })
    }
}